package com.yanqu.road.server.manger.activity.pasture;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.pasture.PastureConfig;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.bussiness.activity.PastureBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.pasture.PastureModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.List;
import java.util.Map;

/**
 * 牧场点兵
 */
public class PastureMgr extends TempMgr {

    private static PastureConfig config;

    // 最大宽度
    public static final int MAX_WIDTH = 9;
    // 最大高度
    public static final int MAX_HEIGHT = 11;
    // 初始化生成的行数
    public static final int INIT_GENERATE_ROW = 2;
    // 操作后可生成的行数
    public static final int OPERATE_GENERATE_ROW = 1;
    // 清空后可生成的行数
    public static final int CLEAR_GENERATE_ROW = 2;
    // 最大保留前几步的数据
    public static final int MAX_KEEP_STEPS = 5;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }


    public static void reloadActivityData() {
        getLogger().info("reload Pasture Activity start");

        ActivityInfo activityInfo;

        // 获取进行中的活动信息
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.Pasture.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no Pasture Activity in show time");
            config = null;
            return;
        } else {
            activityInfo = openActivityInfoList.get(0);
        }

        if (activityInfo != null) {
            // 加载配置
            PastureConfig tmpConfig = new PastureConfig();
            tmpConfig.setActivityInfo(activityInfo);
            int tmpActivityId = activityInfo.getActivityId();
            //config配置
            Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityId);
            tmpConfig.setAnimalConfigMap(PastureBussiness.getPastureAnimalConfigMap(tmpActivityId));
            tmpConfig.setSkillConfigMap(PastureBussiness.getPastureSkillConfigMap(tmpActivityId));
            tmpConfig.setModuleConfigMap(PastureBussiness.getPastureModuleConfigMap(tmpActivityId));
            tmpConfig.setPracticeConfig(PastureBussiness.getPasturePracticeConfigMap(tmpActivityId));
            if (activityConfigMap.containsKey(eActivityConfigType.PASTURE_ENERGY_ITEM_ID.getName())) {
                tmpConfig.setPastureEnergyItemId(activityConfigMap.get(eActivityConfigType.PASTURE_ENERGY_ITEM_ID.getName()).getIntValue());
            }
            if (activityConfigMap.containsKey(eActivityConfigType.PASTURE_SKILL_ITEM_ID.getName())) {
                tmpConfig.setPastureSkillItemId(activityConfigMap.get(eActivityConfigType.PASTURE_SKILL_ITEM_ID.getName()).getIntValue());
            }
            if (activityConfigMap.containsKey(eActivityConfigType.PASTURE_UPGRADE_ITEM_ID.getName())) {
                tmpConfig.setPastureUpgradeItemId(activityConfigMap.get(eActivityConfigType.PASTURE_UPGRADE_ITEM_ID.getName()).getIntValue());
            }
            if (activityConfigMap.containsKey(eActivityConfigType.PASTURE_RESTART_ITEM_ID.getName())) {
                tmpConfig.setPastureRestartItemId(activityConfigMap.get(eActivityConfigType.PASTURE_RESTART_ITEM_ID.getName()).getIntValue());
            }
            if (activityConfigMap.containsKey(eActivityConfigType.PASTURE_SKILL_ITEM_NUM.getName())) {
                tmpConfig.setPastureSkillItemNum(activityConfigMap.get(eActivityConfigType.PASTURE_SKILL_ITEM_NUM.getName()).getIntValue());
            }
            if (activityConfigMap.containsKey(eActivityConfigType.PASTURE_SKILL_ITEM_LIMIT.getName())) {
                tmpConfig.setPastureSkillItemLimit(activityConfigMap.get(eActivityConfigType.PASTURE_SKILL_ITEM_LIMIT.getName()).getIntValue());
            }
            if (activityConfigMap.containsKey(eActivityConfigType.PASTURE_ENERGY.getName())) {
                tmpConfig.setPastureEnergy(activityConfigMap.get(eActivityConfigType.PASTURE_ENERGY.getName()).getIntValue());
            }
            if (activityConfigMap.containsKey(eActivityConfigType.PASTURE_ENERGY_COST.getName())) {
                tmpConfig.setPastureEnergyCost(activityConfigMap.get(eActivityConfigType.PASTURE_ENERGY_COST.getName()).getIntValue());
            }
            if (activityConfigMap.containsKey(eActivityConfigType.PASTURE_SKILL_ENERGY_MAX.getName())) {
                tmpConfig.setPastureSkillEnergyMax(activityConfigMap.get(eActivityConfigType.PASTURE_SKILL_ENERGY_MAX.getName()).getIntListValue("\\|"));
            }
            if (activityConfigMap.containsKey(eActivityConfigType.PASTURE_SKILL_ENERGY_START.getName())) {
                tmpConfig.setPastureSkillEnergyStart(activityConfigMap.get(eActivityConfigType.PASTURE_SKILL_ENERGY_START.getName()).getIntListValue("\\|"));
            }
            if (activityConfigMap.containsKey(eActivityConfigType.PASTURE_POINT_UP_CHAIN.getName())) {
                tmpConfig.setPasturePointUpChain(activityConfigMap.get(eActivityConfigType.PASTURE_POINT_UP_CHAIN.getName()).getIntListValue("\\|"));
            }
            if (activityConfigMap.containsKey(eActivityConfigType.PASTURE_ASSESS_LEVEL.getName())) {
                tmpConfig.setPastureAssessLevel(activityConfigMap.get(eActivityConfigType.PASTURE_ASSESS_LEVEL.getName()).getIntListValue("\\|"));
            }
            if (activityConfigMap.containsKey(eActivityConfigType.PASTURE_ASSESS_REWARD.getName())) {
                tmpConfig.setPastureAssessReward(activityConfigMap.get(eActivityConfigType.PASTURE_ASSESS_REWARD.getName()).getStringListValue("\\|"));
            }
            if (activityConfigMap.containsKey(eActivityConfigType.PASTURE_BOSS_REFRESH.getName())) {
                tmpConfig.setPastureBossRefresh(activityConfigMap.get(eActivityConfigType.PASTURE_BOSS_REFRESH.getName()).getIntListValue("\\|"));
            }
            if (activityConfigMap.containsKey(eActivityConfigType.PASTURE_RESTART_COST.getName())) {
                tmpConfig.setPastureRestartCost(activityConfigMap.get(eActivityConfigType.PASTURE_RESTART_COST.getName()).getValue());
            }
            if (activityConfigMap.containsKey(eActivityConfigType.PASTURE_RESTART_TIME.getName())) {
                tmpConfig.setPastureRestartTime(activityConfigMap.get(eActivityConfigType.PASTURE_RESTART_TIME.getName()).getIntListValue("\\|"));
            }
            if (activityConfigMap.containsKey(eActivityConfigType.PASTURE_PRACTICE_ROUND.getName())) {
                tmpConfig.setPasturePracticeRound(activityConfigMap.get(eActivityConfigType.PASTURE_PRACTICE_ROUND.getName()).getIntValue());
            }
            if (activityConfigMap.containsKey(eActivityConfigType.PASTURE_NORMAL_ROUND.getName())) {
                tmpConfig.setPastureNormalRound(activityConfigMap.get(eActivityConfigType.PASTURE_NORMAL_ROUND.getName()).getIntValue());
            }
            config = tmpConfig;
        }

        getLogger().info("reload Pasture Activity end");

        // 在线玩家下发数据
        for (GamePlayer gamePlayer : GamePlayerMgr.getAllOnlinePlayer()) {
            if (!SystemOpenMgr.systemOpen(gamePlayer, eSystemId.Pasture.getValue())) {
                continue;
            }
            PastureModule module = gamePlayer.getModule(PastureModule.class);
            module.syncConfig();
            module.initUserData();
            module.syncUserData();
        }
    }

    public static PastureConfig getConfig() {
        return config;
    }

    public static int getSkillEnergyMax(int useSkillCnt) {
        if (config == null || config.getPastureSkillEnergyMax() == null) {
            return 0;
        }
        return config.getPastureSkillEnergyMax().get(Math.min(useSkillCnt, config.getPastureSkillEnergyMax().size() - 1));
    }

    /**
     * 复活的道具id
     */
    public static int getPastureRestartItemId() {
        if (config == null) {
            return 0;
        }
        return config.getPastureRestartItemId();
    }

}
